#ifndef _DICOM_ECRYPT_ECIES_H_
#define _DICOM_ECRYPT_ECIES_H_

#include <string>
#include <gwbase/base/status.h>
#include "gwbase/byte_array.h"

// forward declaration
class ECPrivKey;
class ECPubKey;

namespace dicom {

class EciesHelper {
 public:
  ~EciesHelper();
  EciesHelper(const std::string &privkey_hex);

  bool Encrypt(const ByteArray &plain, ByteArray &chipher);
  bool Decrypt(const ByteArray &chiper, ByteArray &plain);

  bool Valid() const { return valid_; }
  const ECPrivKey &PrivKey() const { return *sk_; }
  const ECPubKey &PubKey() const { return *pk_; }

  Status DecryptStorageKeyFromPersonalKey(const std::string &personal_key,
                                          ByteArray *storage_key);

  static bool GenerateRandomPrivateKey(ByteArray &privkey, ByteArray &pubkey_x,
                                       ByteArray &pubkey_y);

  // 服务器返回的是URL Base64编码的私钥
  static EciesHelper *CreateFromPrivateKeyReturnedByServer(
      const std::string &privkey);

  // 解密服务器返回的私钥密文
  static Status TryParsePrivKey(const std::string &b64_pk_str,
                                const std::string &plainpwd, ByteArray *result);

  // 解密服务器返回的私钥明文
  static Status TryParsePlainPrivKey(const std::string &b64_pk_str,
                                     ByteArray *result);

 private:
  ECPrivKey *sk_;
  ECPubKey *pk_;
  bool valid_;
};
}

#endif  // _DICOM_ECRYPT_ECIES_H_
